import { adminService } from '@/services/admin'

const state = {
  users: [],
  songs: [],
  settings: null,
  loading: false,
  error: null,
  pagination: {
    users: {
      page: 1,
      totalPages: 1,
    },
    songs: {
      page: 1,
      totalPages: 1,
    },
  },
}

const getters = {
  users: (state) => state.users,
  songs: (state) => state.songs,
  settings: (state) => state.settings,
  isLoading: (state) => state.loading,
  error: (state) => state.error,
  userPagination: (state) => state.pagination.users,
  songPagination: (state) => state.pagination.songs,
}

const actions = {
  async fetchUsers({ commit }, { page = 1, search = '' } = {}) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      const { users, totalPages } = await adminService.getUsers(page, search)
      commit('setUsers', users)
      commit('setUserPagination', { page, totalPages })
      return { users, totalPages }
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async updateUser({ commit }, { userId, userData }) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      const updatedUser = await adminService.updateUser(userId, userData)
      commit('updateUser', updatedUser)
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async deleteUser({ commit }, userId) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      await adminService.deleteUser(userId)
      commit('removeUser', userId)
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async fetchSongs({ commit }, { page = 1, search = '' } = {}) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      const { songs, totalPages } = await adminService.getSongs(page, search)
      commit('setSongs', songs)
      commit('setSongPagination', { page, totalPages })
      return { songs, totalPages }
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async updateSong({ commit }, { songId, songData }) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      const updatedSong = await adminService.updateSong(songId, songData)
      commit('updateSong', updatedSong)
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async deleteSong({ commit }, songId) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      await adminService.deleteSong(songId)
      commit('removeSong', songId)
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async fetchSettings({ commit }) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      const settings = await adminService.getSettings()
      commit('setSettings', settings)
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
  async updateSettings({ commit }, settings) {
    commit('setLoading', true)
    commit('setError', null)
    try {
      const updatedSettings = await adminService.updateSettings(settings)
      commit('setSettings', updatedSettings)
    } catch (error) {
      commit('setError', error.message)
      throw error
    } finally {
      commit('setLoading', false)
    }
  },
}

const mutations = {
  setUsers(state, users) {
    state.users = users
  },
  setSongs(state, songs) {
    state.songs = songs
  },
  setSettings(state, settings) {
    state.settings = settings
  },
  setLoading(state, loading) {
    state.loading = loading
  },
  setError(state, error) {
    state.error = error
  },
  setUserPagination(state, { page, totalPages }) {
    state.pagination.users = { page, totalPages }
  },
  setSongPagination(state, { page, totalPages }) {
    state.pagination.songs = { page, totalPages }
  },
  updateUser(state, updatedUser) {
    const index = state.users.findIndex((user) => user.id === updatedUser.id)
    if (index !== -1) {
      state.users.splice(index, 1, updatedUser)
    }
  },
  removeUser(state, userId) {
    state.users = state.users.filter((user) => user.id !== userId)
  },
  updateSong(state, updatedSong) {
    const index = state.songs.findIndex((song) => song.id === updatedSong.id)
    if (index !== -1) {
      state.songs.splice(index, 1, updatedSong)
    }
  },
  removeSong(state, songId) {
    state.songs = state.songs.filter((song) => song.id !== songId)
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
}
